package ke04;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class MyFrame extends JFrame {
	
	private Chat chat = new Chat();

	private JLabel lblHeader;
	private JLabel lblUsername;
	private JTextField fldUsername;
	private JButton btnStartChat;
	private JScrollPane scrollList;
	private JList list;
	private JTextField fldMessage;
	private JButton btnSend;

	public MyFrame(String title) {
		super(title);
		createWidgets();
		setupActions();
		layoutWidgets();
	}

	private void setupActions() {
		btnSend.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				String text = fldMessage.getText();

				chat.addMessage(text);

				fldMessage.setText("");
			}
		});

		fldMessage.getDocument().addDocumentListener(new DocumentListener() {

			{
				check();
			}

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				check();
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				check();
			}

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				check();
			}

			private void check() {
				String text = fldMessage.getText();
				btnSend.setEnabled(!text.trim().isEmpty());
			}
		});

		getContentPane().addMouseMotionListener(new MouseMotionListener() {
			
			@Override
			public void mouseMoved(MouseEvent e) {

				if (e.getX() <= 20
						&& e.getY() >= getContentPane().getHeight() - 20) {
					chat.addMessage("Bin AFK");
				}
			}

			@Override
			public void mouseDragged(MouseEvent arg0) {

			}
		});
		
		btnStartChat.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				JOptionPane.showMessageDialog(MyFrame.this, "HAllo!");
				JOptionPane.showConfirmDialog(MyFrame.this, "Test?");
			}
		});
	}

	private void layoutWidgets() {
		setLayout(new GridBagLayout());

		getContentPane().add(lblHeader, new GridBagHelp(0, 0, 3, 1).done());
		getContentPane().add(lblUsername, new GridBagHelp(0, 1).done());
		getContentPane().add(fldUsername,
				new GridBagHelp(0, 2).hfill().done());
		getContentPane().add(btnStartChat,
				new GridBagHelp(0, 3).anchor(GridBagConstraints.NORTH).done());
		getContentPane().add(scrollList,
				new GridBagHelp(1, 1, 2, 3).fill().scale(1, 1).done());
		getContentPane().add(fldMessage,
				new GridBagHelp(1, 4).fill().scale(1, 0).done());
		getContentPane().add(btnSend, new GridBagHelp(2, 4).done());

		pack();

	}

	private void createWidgets() {
		lblHeader = new JLabel("CHAT");
		lblHeader.setFont(lblHeader.getFont().deriveFont(Font.BOLD, 20));
		lblHeader.setHorizontalAlignment(SwingConstants.CENTER);

		lblUsername = new JLabel("Username:");
		fldUsername = new JTextField();
		fldUsername.setMaximumSize(new Dimension(250, 25));

		btnStartChat = new JButton("Start Chat!");

		list = new JList<String>();
		ChatListModel listModel = new ChatListModel();
		chat.addListener(listModel);
		list.setModel(listModel);
		list.setBorder(BorderFactory.createLoweredBevelBorder());

		scrollList = new JScrollPane(list);
		scrollList.setPreferredSize(new Dimension(500, 500));

		fldMessage = new JTextField();

		btnSend = new JButton("Send");

	}

}
